package com.lee.mq.demo;

import com.lee.mq.mqclient.Channel;
import com.lee.mq.mqclient.Connection;
import com.lee.mq.mqclient.ConnectionFactory;
import com.lee.mq.mqserver.core.Binding;
import com.lee.mq.mqserver.core.ExchangeType;

import java.io.IOException;
import java.sql.SQLOutput;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-11-02
 * Time: 20:00
 */
public class DemoProducer {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("生产者启动！");
        // 创建连接和服务器交互
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(9090);
        Connection connection = connectionFactory.newConnection();
        // 创建channel
        Channel channel = connection.createChannel();
//        // 创建交换机(直接)
//        boolean ok = channel.exchangeDeclare("exchange1", ExchangeType.DIRECT,true,false,null);
//        System.out.println("创建交换机："+ok);

//        boolean ok = channel.exchangeDeclare("exchange1",ExchangeType.FANOUT,true,false,null);
//        System.out.println("创建交换机："+ok);
//
//        // 创建队列
//        ok = channel.queueDeclare("queue1",true,false,false,null);
//        System.out.println("创建队列1："+ok);
//        ok = channel.queueDeclare("queue2",true,false,false,null);
//        System.out.println("创建队列2："+ok);
//        // 创建绑定
//        ok = channel.queueBind("exchange1","queue1","");
//        System.out.println("创建绑定："+ok);
        boolean ok = channel.exchangeDeclare("exchange1", ExchangeType.TOPIC,true,false,null);
        System.out.println("交换机创建："+ok);
        // 创建队列
        ok = channel.queueDeclare("queue1",true,false,false,null);
        System.out.println("创建队列1："+ok);
        // 创建绑定
        ok = channel.queueBind("exchange1","queue1","aaa.bbb.ccc");
        System.out.println("创建绑定："+ok);

        // 发送十条消息
        String body = null;
        for (int i = 0; i < 10; i++) {
            body = "hello" + i;
            ok = channel.basicPublish("exchange1","aaa.bbb.ccc",null,body.getBytes());
            System.out.println("发送消息第："+i+": "+ok);
        }

        Thread.sleep(500);
        // 关闭连接
        channel.closeChannel();
        connection.close();
    }
}
